package org.file5.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.file5.service.ServiceFascade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

@Controller
public class XMPPReceiverController {
	private final Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ServiceFascade sf;

	@RequestMapping(value = "/_ah/xmpp/message/chat/", method = RequestMethod.POST)
	//
	public void doXMPPPost(HttpServletRequest req, HttpServletResponse res) {
		XMPPService xmpp = XMPPServiceFactory.getXMPPService();
		Message message;
		/*
		 * The POST request data represents the message as a MIME multipart
		 * message (RFC 2388). Each part is a named POST parameter:
		 * 
		 * from, the address of the sender of the message to, the address of the
		 * recipient as described by the sender (see below) body, the body of
		 * the message stanza, the full XMPP message in its original XML form
		 */
		try {
			message = xmpp.parseMessage(req);
			JID fromJid = message.getFromJid();
			String body = message.getBody();
			String stanza = message.getStanza();
			logger.info("fromJid=" + fromJid);
			logger.info("body=" + body);
			logger.info("stanza=" + stanza);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		logger.info("you got me doXMPPPost()");
	}

}
